"use strict";
Object.defineProperty(exports, "__esModule", {value: true});

var mongoose = require('mongoose');
var Q = require('q');

var Schema = mongoose.Schema;

//定义 message 结构
let schemaMessage = new Schema({
    content: {
        type: 'String',
        required: true //内容 非空
    },
    type: {
        type: 'String',
        default: 'normal'
    },
    fromID: {
        type: 'String',
        required: true
    },
    from: {
        type: 'String',
        required: true
    },
    toID: {
        type: 'String',
        required: true
    },
    to: {
        type: 'String',
        required: true
    },
    timeStamp: {
        type: Date,
        default: Date.now
    }
});
exports.schemaMessage = schemaMessage;

let Messages = mongoose.model('Messages', schemaMessage);
exports.Messages = Messages;


//插入一条消息
exports.insertMessages = function (message) {
    console.log("insertMessage");
    this.m = message;
    console.log(message);

    //防止错误时破坏数据
    if (this.m.content == null || typeof (this.m.content) == 'undefined') {
        console.log("缺少 content ");
        return;
    }
    if (this.m.fromID == null || typeof (this.m.fromID) == 'undefined') {
        console.log("缺少 fromID ");
        return;
    }
    if (this.m.from == null || typeof (this.m.from) == 'undefined') {
        console.log("缺少 from ");
        return;
    }

    //var Message = mongoose.model('Messages', schemaMessage);
    var m = new Messages;

    m.content = this.m.content;
    m.type = this.m.type;
    //先不要 m.color = this.m.color

    m.fromID = this.m.fromID;
    m.from = this.m.from;
    if (typeof (this.m.toID) == 'undefined' || this.m.toID == null) {
        //直接设置为 群聊 userID: 0, userName: "all", nickname: "所有人"
        m.toID = '0';
        m.to = 'all';
    }
    else {
        m.toID = this.m.toID;
        m.to = this.m.to;
    }


    console.log("mmmm==");
    console.log(m);
    console.log("mmmm22==");
    m.save(function (err) {
        if (err != null) {
            console.log("err777");
            console.log(err);
        }
    });

};

//查询聊天信息:需要哪一页?多少条?--目前没有用到，待 admin 用到
exports.queryMessages = function (page, rows) {
    var deferred = Q.defer();
    if (!dbConnected) {
        //数据库错误,直接返回
        console.log('数据库错误,直接返回 =');
        deferred.reject("<h1> 数据库错误,请检查数据库服务器是否开启</h1>");
        return deferred.promise;
    }
    ;

    //var Messages = mongoose.model('Messages', schemaMessage);
    var where = {}; //查询条件     //找到所有数据
    var projection = {'_id': 0};//返回所有字段,除了_id

    if (page <= 0 || typeof(page) == 'undefined') page = DEFAULT_START_PAGENO;
    if (rows <= 0 || typeof(rows) == 'undefined') rows = DEFAULT_ROWS;
    if (rows > MAX_PAGE_SIZE) rows = MAX_PAGE_SIZE;

    var query = Messages.find(where);
    //跳过 前面页的记录
    query.skip((page - 1) * rows);
    //限制总共获取的条数
    query.limit(rows);

    query.exec(function (err, messages) {
        if (err == null) {
            console.log("找到了messages：");
            //console.log(messages);
            deferred.resolve(messages);
        }
        else {
            deferred.reject('error!No messages');
        }
    });

    return deferred.promise;
};

